function isLegal(){
    var flag = true;
    var account = document.forms["login_form"]["account"].value;
    var isnum = /^\d+$/.test(account);
    if (!isnum || account.length!=11){
        var ap = document.getElementById("account_");
        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAccount does not exist";
//        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp账号不存在";
        ap.removeAttribute("hidden");
        flag = false;
    }
    else{
        var ap = document.getElementById("account_");
        ap.setAttribute("hidden","true");
    }

    var pwd = document.forms["login_form"]["pwd"].value;
    if (pwd.length<6||pwd.length>30){
        var ap = document.getElementById("pwd_");
        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWrong password";
//        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp密码不正确";
        ap.removeAttribute("hidden");
        flag = false;
    }
    else{
        var ap = document.getElementById("pwd_");
        ap.setAttribute("hidden","true");
    }
    return flag;
}

function check_sign(){
    var flag = true;
    var account = document.forms['sign_form']['account'].value;
    var isnum = /^\d+$/.test(account);

    if (!isnum || account.length!=11){
        var ap = document.getElementById("account_");
        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspPlease input the correct mobile phone number";
//        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp请输入正确的手机号";
        ap.removeAttribute("hidden");
        flag = false;
    }
    else{
        var ap = document.getElementById("account_");
        ap.setAttribute("hidden","true");
    }
    var name = document.forms['sign_form']['username'].value;
    name.replace(/^\s+|\s+$/g, '');

    if (name.length==0||name.length>10){
        var ap = document.getElementById("username_");
        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspThe length of nickname is 1-9 bytes";
//        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp昵称长度为1~9字节";
        ap.removeAttribute("hidden");
        flag = false;
    }
    else{
        var ap = document.getElementById("username_");
        ap.setAttribute("hidden","true");
    }

    var pwd1 = document.forms['sign_form']['pwd'].value;
    var pwd2 = document.forms['sign_form']['pwd2'].value;

    if (pwd1.length<6){
        var ap = document.getElementById("pwd_");
        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspThe password should be 6-30 digits";
//        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp密码应为6~30位";
        ap.removeAttribute("hidden");
        flag = false;
    }
    else if (pwd1!=pwd2){
        var ap = document.getElementById("pwd_");
        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspThe password is different";
//        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp密码不同";
        ap.removeAttribute("hidden");
        flag = false;
    }
    else{
        var ap = document.getElementById("pwd_");
        ap.setAttribute("hidden","true");
    }
    return flag;
}



function validateLoginForm(){

    if(!isLegal())
        return false;

    var md5_pwd= document.getElementById('md5_pwd');
    var pwd = document.forms["login_form"]["pwd"].value;
    md5_pwd.value = md5(pwd);
    var account = $('#account').val()
    var pwd = $('#md5_pwd').val()
    var orember = document.getElementById("rember");
    if(orember.checked){
        setCookie('account',account,7); //保存帐号到cookie，有效期7天
    }
    $.ajax({
        url:'/login',
        type:'post',
        data:{account:account,md5_pwd:pwd},
        success: function(data){
        if(data['code']==1){
            $('#md5_pwd').val('')
            $('#pwd').val('')
            $('#pwd_').html('&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp'+data['mesg'])
            $('#pwd_').removeAttr("hidden")
            }
        else if(data['code']==2){
               location.href = data['mesg'];
            }
        },
        error:function(error){
            console.log(error)
        }
    });
    return true;
}



function validateSignForm(){
    if(!check_sign())
        return false;
    var md5_pwd= document.getElementById('md5_pwd');
    var pwd = document.forms["sign_form"]["pwd"].value;
    md5_pwd.value= md5(pwd);
    var account = $('#account').val()
    var pwd = $('#md5_pwd').val()
    var nickname = $('#nickname').val()
    $.ajax({
        url:'/sign',
        type:'post',
        data:{account:account, md5_pwd:pwd, nickname:nickname},
        success: function(data){
            if(data['code']==1){
                $('#md5_pwd').val('')
                $('#pwd').val('')
                $('#pwd2').val('')
                $('#pwd_').html('&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp'+data['mesg'])
                $('#pwd_').removeAttr("hidden")
            }
        else if(data['code']==2){
                location.href = data['mesg'];
             }
        },
        error:function(error){
            console.log(error)
        }
    });
    return true;
}


function validateFindForm(){
    var account = document.forms["find_form"]["account"].value;
    var isnum = /^\d+$/.test(account);
    if (!isnum||account.length!=11){
        var ap = document.getElementById("account_");
        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspPlease input the correct mobile phone number";
//        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp请输入正确的手机号";
        ap.removeAttribute("hidden");
        return false;
    }
    return true;
}


function subTele(){
    var account = document.forms["find_form"]["account"].value;
    var isnum = /^\d+$/.test(account);
    if (!isnum||account.length!=11){
        var ap = document.getElementById("account_");
        //ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp请输入正确的手机号";
        ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspPlease input the correct mobile phone number";
        ap.removeAttribute("hidden");
        return false;
    }
    var path = window.location.href.toString();
    var last = path.lastIndexOf("/")
    var newPath = path.substring(0,last) + "/find_pwd";
    $.post(newPath,{account:account},
        function(data){
            if (data != '1'){
                var ap = document.getElementById("account_");
                ap.innerHTML= "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + data;
                ap.removeAttribute("hidden");
            }
            else{
                var ac = document.getElementById("account_");
                ac.setAttribute("hidden","true");
                var ap = document.getElementById("vcode_div");
                ap.removeAttribute("hidden");
                var log = document.getElementById("log");
                log.removeAttribute("hidden");
                var log = document.getElementById("get_vcode");
                log.setAttribute("hidden","true");
                var log = document.getElementById("j_log");
                log.setAttribute("hidden","true");
            }function setCookie(name,value,day){
                 var date = new Date();
                 date.setDate(date.getDate() + day);
                 document.cookie = name + '=' + value + ';expires='+ date;
             }
        }
    );
}

function setCookie(name,value,day){
    var date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires='+ date;
}
